#include "GRO_Gui.h"
#include <iostream>
#include "SDL_image.h"
#include <unistd.h>
using namespace std;

#include "GRO_Backend_Sprig.h"
#include "SDL_ttf.h"
#include "GRO_Font_NFont.h"

SDL_Surface* screen;


NFont font1;
NFont font2;
GRO::GRO_Font* blackFont = NULL;
GRO::GRO_Font* whiteFont = NULL;



void setbool_callback(bool* ptr, bool value)
{
    fflush(stdout);
    if(ptr != NULL)
        *ptr = value;
}


void start()
{
    using namespace GRO;
    
    Gui& gui = Gui::instance();
    
    Uint32 frametime;
    float targetFps = 60;
    float waitTime = 1000.0f / targetFps;
    
    int mx = 0, my = 0;
    
    bool done = false;
    
    // Add QUIT shortcut
    Shortcut* shortcut = new Shortcut(KMOD_CTRL, SDLK_q);
    shortcut->onExecute.connect<void, bool*, bool>(setbool_callback, &done, true);
    gui.addShortcut(shortcut);
    
    
    SDL_Event event;
    int frameStart;
    while (!done)
    {
        frameStart = SDL_GetTicks();
        SDL_GetMouseState(&mx, &my);
        
        while (SDL_PollEvent(&event))
        {
            if (event.type == SDL_QUIT)
                done = true;
            
            if(gui.event(event) != NULL)
                continue;
            

            if (event.type == SDL_KEYDOWN)
            {
                
            }
        }
        
        gui.update();
        
        SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 100, 100, 160));
        
        gui.draw();
        
        
        SDL_Flip(screen);
        
        frametime = SDL_GetTicks() - frameStart;

        if (SDL_GetTicks() - frameStart < waitTime)
            SDL_Delay((int)(waitTime - frametime));

    }
    
    
}




void init()
{
    
    GRO::EnableUnicode();
    GRO::KeyRepeat();
    
    TTF_Font* ttf = TTF_OpenFont("../fonts/FreeSans.ttf", 14);
    SDL_Color fgColor = {0, 0, 0, SDL_ALPHA_OPAQUE};
    font1.load(ttf, fgColor);
    font1.setDest(screen);
    
    TTF_CloseFont(ttf);
    
    font2.load(screen, NFont::verticalGradient(font1.getSurface(), 0xffffff, 0x999999, 0));
    
    blackFont = new GRO::GRO_Font_NFont(&font1);
    whiteFont = new GRO::GRO_Font_NFont(&font2);
}

void loadGui()
{
    using namespace GRO;
    
    
    Gui& gui = Gui::instance();
    gui.init(screen->format, screen->w, screen->h);
    
    
    Window* window = new Window(300, 150, 400, 300, "My Window", whiteFont);
    
    gui.add(window);
    
    window->setGridSize(4, 4);
    
    window->set(0,0, new Text("Foods I Like:", blackFont));

    window->set(1,0, new Checkbox(20, 20, true));

    window->set(1,1, new Text(" Pizza", blackFont));
    
    window->set(2,0, new Checkbox(20, 20, false));

    window->set(2,1, new Text("Broccoli", blackFont));

    // Editable text
    Group* group = new Group(200, 200);
    
    ScrollBar* scrollbar = new ScrollBar(15, group->area.h - 20, 15, 10, 15, 15, DOWN);

    scrollbar->setDock(DOCK_UP_RIGHT);
    scrollbar->padding.up = 10;
    group->pin(scrollbar);
    group->setScroll(scrollbar, DOWN);
    
    Text* text = new Text("I like this one because:\n", blackFont);
    text->editable = 1;
    text->textMax = 1000;
    text->area.w = 170;
    text->area.h = 400;
    
    window->set(3, 1, group);
    group->set(0, 0, text);
    window->fitContents();
}


int main(int argc, char* argv[])
{
    
    char buff[] = "SDL_VIDEO_CENTERED=center";
    
    SDL_putenv(buff);
    
    screen = SPG_InitSDL(800, 600, 0, SDL_INIT_VIDEO, SDL_SWSURFACE);
    if(screen == NULL)
        return 0;
    
    SDL_WM_SetCaption("Starter Code", NULL);
    
    TTF_Init();
    
    GRO::GRO_SetRenderer(new GRO::Renderer_Sprig(screen));
    
    srand(time(NULL));
    
    init();
    loadGui();
    start();
    
    
    SDL_Quit();
    return 0;
}
